<script setup lang="ts">
import { ref } from 'vue';
import { useWorkflowStore } from '@/store/workflow';
import { nodeTypes } from '@/config/nodeTypes';
import { ElMessage } from 'element-plus';

const workflowStore = useWorkflowStore();

// 节点类型分类
const nodeCategories = ref({});

// 初始化节点类型分类
Object.entries(nodeTypes).forEach(([type, config]) => {
  const category = config.category || '其他';
  if (!nodeCategories.value[category]) {
    nodeCategories.value[category] = [];
  }
  nodeCategories.value[category].push({
    type,
    ...config
  });
});

// 开始拖动节点
function startDrag(event, nodeType, nodeLabel) {
  if (event.dataTransfer) {
    // 设置拖动数据
    event.dataTransfer.setData('application/node-type', nodeType);
    event.dataTransfer.setData('application/node-label', nodeLabel);
    event.dataTransfer.effectAllowed = 'copy';
  }
}
</script>

<template>
  <div class="node-library">
    <h3>节点库</h3>
    
    <div v-for="(nodes, category) in nodeCategories" :key="category" class="node-category">
      <h4>{{ category }}</h4>
      
      <div class="node-list">
        <div 
          v-for="node in nodes" 
          :key="node.type"
          class="node-item"
          :style="{ borderColor: node.color }"
          draggable="true"
          @dragstart="startDrag($event, node.type, node.label)"
        >
          <el-icon v-if="node.icon"><component :is="node.icon" /></el-icon>
          <span>{{ node.label }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.node-library {
  padding: 16px;
  height: 100%;
  overflow-y: auto;
  background-color: #f5f7fa;
}

.node-category {
  margin-bottom: 16px;
}

.node-category h4 {
  margin: 8px 0;
  padding-bottom: 4px;
  border-bottom: 1px solid #dcdfe6;
  color: #606266;
}

.node-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.node-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background-color: white;
  border: 2px solid;
  border-radius: 4px;
  cursor: move;
  user-select: none;
  transition: all 0.2s;
}

.node-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.node-item .el-icon {
  font-size: 16px;
}
</style>